#include <assgl.h>

#ifdef  __cplusplus
extern "C" 
{
#endif  //__cplusplus

#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>


#ifdef  __cplusplus
}
#endif  //__cplusplus

static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
                     char *filename)
{
    FILE *f;
    int i;

    f = fopen(filename,"wb");
    fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
    for (i = 0; i < ysize; i++)
        fwrite(buf + i * wrap, 1, xsize, f);
    fclose(f);
}

// The MAIN function, from here we start our application and run the Game loop
int main()
{
    AssglLib* lib = assgl_read_file("www.ass");
    if (!lib){
        printf( "assgl_read_file has err\n");    
    }
    printf( "PlayResX:%d\n" , lib->PlayResX );
    printf( "PlayResY:%d\n" , lib->PlayResY );
    
    wprintf( "Text:%s\n" , lib->AssglTrackItem[0].Text );
    openglInit(lib);

    //读取图片，获取数据到AVFrame中
    AVFormatContext *pFormatCtx = avformat_alloc_context();
    AVCodec *pCodec;
    AVFrame *pFrame;
    AVPacket packet;
    int frameFinished;
    av_register_all();

    // 打开输入图片文件
    if (avformat_open_input(&pFormatCtx,"picture2.jpg",NULL,NULL) != 0) {
        printf("Cannot open input file\n");
        return 1;
    }
 
    // 找到解码器并打开
    if (avformat_find_stream_info(pFormatCtx,NULL) < 0) {
        printf("Cannot find stream information\n");
        return 1;
    }
 
    // 为图片分配解码器上下文
    pCodec = avcodec_find_decoder(pFormatCtx->streams[0]->codec->codec_id);
    if (pCodec == NULL) {
        printf("Unsupported codec!\n");
        return 1;
    }
 
    if (avcodec_open2(pFormatCtx->streams[0]->codec, pCodec, NULL) < 0) {
        printf("Cannot open codec\n");
        return 1;
    }
 
    // 为图片帧分配内存
    pFrame = av_frame_alloc();

    // 读取图片到AVFrame
    int ret;
    while (av_read_frame(pFormatCtx, &packet) >= 0)
    {
        ret = avcodec_send_packet(pFormatCtx->streams[0]->codec, &packet);
        if (ret < 0)
        {
            fprintf(stderr, "Error sending a packet for decoding\n");
            exit(1);
        }
        char buf[1024];
        while (ret >= 0)
        {
            ret = avcodec_receive_frame(pFormatCtx->streams[0]->codec, pFrame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                return;
            else if (ret < 0)
            {
                fprintf(stderr, "Error during decoding\n");
                exit(1);
            }

            printf("saving frame %3d\n", pFormatCtx->streams[0]->codec->frame_number);
            fflush(stdout);

            /* the picture is allocated by the decoder. no need to
               free it */
            snprintf(buf, sizeof(buf), "%s-%d", "xxxx", pFormatCtx->streams[0]->codec->frame_number);
            pgm_save(pFrame->data[0], pFrame->linesize[0],
                     pFrame->width, pFrame->height, buf);
        }
    }

    // 释放资源
    av_free_packet(&packet);
    avcodec_close(pFormatCtx->streams[0]->codec);
    // av_free(pFrame);
    avformat_close_input(&pFormatCtx);


    AssglImage * bitmap = assgl_render_frame(lib,5);

    //============test 2===========
    FILE *fd = fopen("testpng6.png","w+");
    fwrite(bitmap->bitmap,bitmap->stride,1,fd);
    fclose(fd);
    //============test 2 end===========
 
    return 0;
}
